package com.net.oil.news;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.news.bean.NewsListBean;
import com.net.oil.util.StringUtil;
import com.net.oil.util.WebViewUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewsDetailActivity extends AppCompatActivity {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.web_view)
    WebView webView;

    private static String mimeType = "text/html; charset=UTF-8";

    private WebSettings webSetting;
    private NewsListBean.NewsBean news;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        news = (NewsListBean.NewsBean) intent.getSerializableExtra(Constant.INTENT_NEWS_KEY);
        if (news != null) {
            if (!StringUtil.isEmpty(news.getContent())) {
                webView.loadData(WebViewUtil.getHtmlData(news.getContent()), mimeType, null);
            }
        }

    }

    private void initView() {
        toolbarTitle.setText(getString(R.string.news_detail));
        toolbarGoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        webSetting = webView.getSettings();
        webSetting.setTextZoom(100);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setUseWideViewPort(true);  //将图片调整到适合webview的大小
        webSetting.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

//        webView.loadData(test, mimeType, encoding);
    }

    @OnClick(R.id.toolbar_go_back)
    public void onViewClicked() {
        finish();
    }

}
